package com.linxiao.hrcrm.sms.manager.impl;

import cn.hutool.core.thread.ThreadUtil;
import com.alibaba.fastjson2.JSON;
import com.linxiao.hrcrm.sms.domain.SmsSendRecords;
import com.linxiao.hrcrm.sms.manager.SmsManager;
import com.linxiao.hrcrm.sms.service.ISmsSendRecordsService;
import com.linxiao.hrcrm.sms.utils.DahanSms;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 大汉短信发送
 * @author zhoudong
 */
@Service("dahanSmsManager")
public class DahanSmsManagerImpl implements SmsManager {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private ISmsSendRecordsService smsSendRecordsService;

    /**
     * 短信发送
     * @param mobile
     * @param content
     * @return
     */
    @Override
    public String send(String mchId, String mobile, String content) {

        // 发送短信
        String send = DahanSms.send(mobile, content);

        // 保存发送记录
        ThreadUtil.execute(() -> {
            SmsSendRecords smsSendRecords = new SmsSendRecords();
            smsSendRecords.setMchId(mchId);
            smsSendRecords.setSender(mchId);
            smsSendRecords.setContent(content);
            smsSendRecords.setRecipient(mobile);
            smsSendRecords.setStatus("SENT");
            logger.info("短信发送记录：{}", JSON.toJSONString(smsSendRecords));
            smsSendRecordsService.insertSmsSendRecords(smsSendRecords);
        });
        return send;
    }
}
